<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Admin\Renderable\BannerRender;
use App\Admin\Renderable\IconRender;
use App\Models\WxIcon;
use App\Models\WxSlideshow;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formCircleTrait{
    public function index_circle(Content $content)
    {
        return $content->title(env('circle_call', '圈子').'相关')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_circle()->render()}</div>
HTML
            );
    }
    protected function form_circle(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();
        global $__MINI_GLOBAL_TENANT_ID__;
        if($__MINI_GLOBAL_TENANT_ID__ === 0){
            $form->tab('新建'.env('circle_call', '圈子'), function (Form $form) {
                $form->html(function () {
                    return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">小提示: 圈子和圈主名称自定义后，前端需要重新下载更新才起作用</div>';
                }, '');
                $form->text('app_circle_call_name', '圈子')->help('名称自定义：如社团')->default(Settings::get('app_circle_call_name', '圈子'));
                $form->text('app_circle_user_call_name', '圈主')->help('名称自定义：如社长')->default(Settings::get('app_circle_user_call_name', '圈主'));
            });
        }
        $form->tab('新建'.env('circle_call', '圈子'), function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">小提示: 在用户点击提交之后才会检测用户的权限</div>';
            }, '');
            $form->radio('app_create_circle_permissions', '建圈权限')->options(FieldUtils::userPermissions())->default(Settings::get('app_create_circle_permissions', 0, true));
        });

        $form->tab(env('circle_call', '圈子').'页', function (Form $form) {

            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">格子菜单说明：<br>1、'.env('circle_call', '圈子').'页的小方格，分为全局和'.env('circle_call', '圈子').'页独立的，两者合并起作用，<a href="https://img.mini.minisns.cn/hbx/circle_square.png" target="_blank">查看效果</a><br>2、圈子页独立的格子菜单在圈子管理页配置</div>';
            }, '');
            $form->multipleSelectTable('app_circle_square_list', '全局-格子菜单')
                ->title('图标库')
                ->max(10)
                ->from(IconRender::make())
                ->model(WxIcon::class, 'id', 'name', 'selectTable:formCircleTrait:app_circle_square_list')
                ->help(env('circle_call', '圈子').'页面的格子菜单， <a href="'.admin_url('settings/icons').'" target="_blank">管理图标库>></a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_circle_square_list', '', true));

            $form->radio('app_circle_list_publish_icon', '底部发布图标')->options([0=>'居中式', 1 => '贴右侧'])->help(env('circle_call', '圈子').'页的底部发布图标')->default(Settings::get('app_circle_list_publish_icon', 0, true));
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">计算热度说明：<br>1、每次发布新内容时，都给这个笔记一个热度值，这个热度值是跟当前时间成正比，也就是越新发布的，热度值越大<br>2、如果有评论，喜欢等互动，就给这个热度值增加相应的热度<br>3、评论，喜欢等互动越来越多的时候，后面增加的热度值越来越小，但不会小到0。<br>4、效果是新发表的内容有稳定的曝光，同时增加互动量高的笔记曝光时间</div>';
            }, '');
            $form->listbox('app_circle_list_sort', '内容列表排序')->options([
                'weight' => '计算热度',
                'created_at'=>'发表时间',
                'last_reply_at' => '最后回复时间',
                'id' => '文章ID'
            ])->help('右边代表选中')->customFormat(function ($value){
                return Settings::get('app_circle_list_sort', ['id'], true);
            });
            $form->radio('app_circle_list_style', '全局-内容列表样式')->options([0=>'默认',1=>'瀑布流'])->default(Settings::get('app_circle_list_style', 0, true));

        });

        $form->tab('全部'.env('circle_call', '圈子'), function (Form $form) {
            $form->html(function () {
                return '<a href="https://img.mini.minisns.cn/images/circle_all_banner.jpg" target="_blank">轮播图效果</a>';
            }, '');

            $form->multipleSelectTable('app_circles_banner', '轮播图')
                ->title('轮播图库')
                ->max(10)
                ->from(BannerRender::make())
                ->model(WxSlideshow::class, 'id', 'id', 'selectTable:formCircleTrait:app_circles_banner')
                ->help('<a href="'.admin_url('slideshow').'" target="_blank">运营管理-轮播图中心</a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_circles_banner', '', true));
        });

        if($__MINI_GLOBAL_TENANT_ID__ === 0){
            $form->tab('付费'.env('circle_call', '圈子'), function (Form $form) {
                $form->html(function () {
                    return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">用户付费加入'.env('circle_call', '圈子').'，可设置平台的分成比例，余下的为'.env('circle_user_call', '圈主').'所有</div>';
                }, '');
                $form->rate('app_circle_follow_pay_division', '平台分成')->default(Settings::get('app_circle_follow_pay_division', 20));
            });
        }


        $form->action('settings/action/circle');
        return $form;
    }

    public function circle(Request $request){
        global $__MINI_GLOBAL_TENANT_ID__;
        SiteUtils::forget_config_data();
        Settings::set('app_circle_square_list', AdminUtils::_multipleSelectTable_order_process('selectTable:formCircleTrait:app_circle_square_list', $request->input('app_circle_square_list')), true, true);

        Settings::set('app_circle_list_publish_icon', $request->input('app_circle_list_publish_icon'), true, true);
        Settings::set('app_circle_list_sort', $request->input('app_circle_list_sort'), true, true);
        Settings::set('app_circles_banner', AdminUtils::_multipleSelectTable_order_process('selectTable:formCircleTrait:app_circles_banner', $request->input('app_circles_banner')) , true, true);
        Settings::set('app_create_circle_permissions', $request->input('app_create_circle_permissions'), true, true);

        if($__MINI_GLOBAL_TENANT_ID__ === 0){

            Settings::set('app_circle_call_name', trim_all_space($request->input('app_circle_call_name', '圈子')), true);
            Settings::set('app_circle_user_call_name', trim_all_space($request->input('app_circle_user_call_name', '圈主')), true);

            _update_env([
                'circle_call' => trim_all_space($request->input('app_circle_call_name', '圈子')),
                'circle_user_call' => trim_all_space($request->input('app_circle_user_call_name', '圈主'))
            ], [], 1);

        }

        Settings::set('app_circle_list_style', $request->input('app_circle_list_style'), true, true);
        if($__MINI_GLOBAL_TENANT_ID__ === 0){
            Settings::set('app_circle_follow_pay_division', _between_($request->input('app_circle_follow_pay_division'), 0, 100), true);
        }


        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
